import wepy from 'wepy'
export default class extends wepy.mixin {
  data = {
    value: '',
    searchList: [],
    kwList: []
  }
  computed = {
    isFlag() {
      if (this.value.length <= 0) {
        return true
      } else {
        return false
      }
    }
  }
  methods = {
    onSearch(e) {
      console.log(e.detail)
      if (e.detail.trim().length <= 0) {
        return
      }
      if (this.kwList.indexOf(e.detail.trim()) === -1) {
        this.kwList.unshift(e.detail.trim())
      }
      this.kwList = this.kwList.slice(0, 10)
      wepy.setStorageSync('kw', this.kwList)
      wepy.navigateTo({
        url: `/pages/goods_list?query=${e.detail.trim()}`
      })
    },
    onCancel() {
      this.searchList = []
    },
    onChange(e) {
      this.value = e.detail.trim()
      if (e.detail.trim().length <= 0) {
        this.searchList = []
        return
      }
      this.getSearchList(e.detail)
    },
    goGoodDetail(id) {
      wepy.navigateTo({
        url: `/pages/goods_detail/main?goods_id=${id}`
      })
    },
    goGoodsDetail(value) {
      wepy.navigateTo({
        url: `/pages/goods_list?query=${value}`
      })
    },
    deleteList(){
        this.kwList = []
        wepy.setStorageSync('kw', this.kwList)
    }
  }
  onLoad() {
    this.kwList = wx.getStorageSync('kw') || []
  }
  async getSearchList(e) {
    const { data: res } = await wepy.getData('/goods/qsearch', { query: e })
    console.log(res)
    this.searchList = res.message
    this.$apply()
  }
}
